<?php
	session_start();
	if(!($_SESSION["username"])||($_SESSION["role"]!="TUT")){
		//not logged in or tutor
		header("location:index.php");
	}
	
	$id=$_GET['id'];
	
	// Connect to server and select databse.
	mysql_connect("localhost", "root", "")or die("cannot connect"); 
	mysql_select_db("Project 5.5")or die("cannot select DB");
	
	$sql='SELECT * FROM classes WHERE Class_ID="'.$id.'"';
	$result=mysql_fetch_row(mysql_query($sql));
	
	
		
	if(isset($_GET['success'])){
		
		$students=$_POST['classStudents'];
		
		$sql3="SELECT User_ID FROM studentinfo WHERE Class_ID='".$id."'";
		$result3=mysql_query($sql3);
		
		while ($row3 = mysql_fetch_row($result3)) {
			if (!in_array($row3[0],$students)) {
				$sql="UPDATE studentinfo SET Class_ID='0' WHERE User_ID = '".$row3[0]."'";
				$result=mysql_query($sql);
			}
		}		
		
		//updates class information with new values
		foreach ($students as $s) {
			$sql2="SELECT Class_ID FROM studentinfo WHERE User_ID = '".$s."'";
			$result2=mysql_fetch_row(mysql_query($sql2));
			echo 'PrevClass: '.$result2[0].'<br/>';
			
			if(($result2[0]!=0)&($result2[0]!=$id)){
				$sql="SELECT Size FROM classes WHERE Class_ID='".$result2[0]."'";
				$result=mysql_fetch_row(mysql_query($sql));
				echo 'oldsize: '.$result[0].'<br/>';
				
				$size=(int)$result[0]-1;
				echo 'newSize: '.$size.'<br/>';
				
				$sql2="UPDATE classes SET Size='".$size."' WHERE Class_ID='".$result2[0]."'";
				$result2=mysql_query($sql2);
				echo 'Sql2- '.$sql2.'<br/>';
			}
			
			$sql="UPDATE studentinfo SET Class_ID='".$id."' WHERE User_ID = '".$s."'";
			$result=mysql_query($sql);
		}
		
		$sql='UPDATE classes SET Tutor="'.$_POST['tutor'].'",Location="'.$_POST['location'].'",Size="'.count($students).'",Day="'.$_POST['weekday'].'",Time="'.$_POST['time'].'" WHERE Class_ID = "'.$id.'"';
		$result=mysql_query($sql);
		
		header("location:manageclasses.php?message=edit");
	}
	
	//exports students into a dropdown
	function listStudents(){
		
		$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
		$objDB = mysql_select_db("Project 5.5");
		
		$sql="SELECT User_ID, First_Name, Last_Name FROM users WHERE Role='STU'";
		$result=mysql_query($sql);
		
		while ($row = mysql_fetch_row($result)){
			$sql2="SELECT Class_ID FROM studentinfo WHERE User_ID='".$row[0]."'";
			$result2=mysql_query($sql2);
			$option = "<OPTION value='".$row[0]."'";
			$row2 = mysql_fetch_row($result2);
			if ($row2[0]==$_GET['id']) {
				$option .= " selected='selected'";
			}
			$option .= ">".$row[1]." ".$row[2]."</OPTION>\n";
			echo $option;
		}
	}
	
	//exports tutors into a dropdown
	function listTutors(){
				
		$sql="SELECT User_ID, First_Name, Last_Name FROM users WHERE Role='TUT'";
		$result= mysql_query($sql);
		
		while ($row = mysql_fetch_row($result)){
			$sql2="SELECT Tutor FROM classes WHERE Class_ID='".$_GET['id']."'";
			$result2=mysql_query($sql2);
			$option = "<OPTION value='".$row[0]."'";
			$row2 = mysql_fetch_row($result2);
			if ($row2[0]==$row[0]) {
				$option .= " selected='selected'";
			}
			$option .= ">".$row[1]." ".$row[2]."</OPTION>\n";
			echo $option;
		}			
		
	}
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>Edit Class</title>	
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<link href="css/main.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="js/formsValidate.js"/></script>
</head>
<body>
	<div id="topbar">
		<div id="qut_logo" />  
		<img src="../img/strapline.png" alt="Slogan" id="slogan"/>
	</div>
	<div id="menubar">
		<div id="breadcrumbs">
			<a href="index.php">Home</a> \ <a href="management.php">Management</a> \ <a href="manageclasses.php">Classes</a> \ <a href="editclass.php?id=<?php echo $id ?>">Edit Class</a>
		</div>
		<div id="logout"> 
			<a href="login.php?destroy=true">Log Out</a>
		</div>
	</div>
	<a href="editclass.php?id=<?php echo $id ?>">
		<div id="error">
		</div>
	</a>
	<div style="" id="namebar_editclass" ></div>
	<div id="body">
	<br/>
		Make your changes and click save to continue.
		<br/><br/>
		<center>
		
		<form action="editclass.php?id=<?php echo $id ?>&success=true" method="post" name="editclassform" id="editclassform" onsubmit="return validateForm('editclassform');">
			<div style="text-align:left;width:460px;background:#fff;">
				<div style="background-color: #CCC;font-size:25pt;font-weight:bold;text-align:center;"><?php echo "Edit Class Details" ?></div><br/>
				Tutor:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				<SELECT name="tutor" style="width:155px;">
					<option value="blank"></option>
					<?php
						listTutors();
					?>
				</SELECT>
				&nbsp;&nbsp;&nbsp;&nbsp;Students:
				<SELECT multiple="multiple" style='float:right;left:-5px;position:relative;top:-10px;width:160px;height:150px;' name="classStudents[]">
					<?php
						listStudents();
					?>
				</SELECT><br/>
				Location:<input type='text' name='location' value='<?php echo $result[2] ?>'/><br/>
				Day:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' name='weekday' value='<?php echo $result[4] ?>'/><br/>
				Time:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' name='time' value='<?php echo $result[5] ?>'/><br/>
				<center><br/>
				<input class="subBtn" type='submit' value=''/>
				<a href="manageclasses.php">
				<input class="CancelBtn" type="button" name="cancel" value="" />
				</a></center>
			</div>
		</form>
		</center>
		<br/><br/>
	</div>
	
	<div id="bottombg">
		<p>
			<a style="color:#333" href="http://www.qut.edu.au/additional/privacy">Privacy</a> 
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/copyright">Copyright</a>
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/disclaimer">Disclaimer</a>
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/accessibility">Accessibility</a>
		</p>
	</div>
</body>
</html>